*******************************************************************************
* Purpose: Heterogeneity graphs
*******************************************************************************

do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 


********************************************************************************
* Figure 3: Heterogeneity: Child parity, age of the mother and prior mental health issues
********************************************************************************

// Pre-birth contact with mental health care
use "$work\any1_pre",clear // load monthly estimates
gen m = 1 // variable for month of estimate
forvalue v = 2/24 {
append using "$work\any`v'_pre"
replace m = `v' if m == .
}
keep if parm == "interaction"
save "$work\any_pre_int", replace // these are from the interacted models to test for differences being significant

use "$work\any1_pre0",clear // similar stuff but for the split sample estimates
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_pre0"
replace m = `v' if m == .
}
keep if parm == "peer_treat"
save "$work\any_pre0", replace

use "$work\any1_pre1",clear // counterpart group
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_pre1"
replace m = `v' if m == .
}
keep if parm == "peer_treat"
save "$work\any_pre1", replace

use "$work\any_pre_int",clear // collect all estimates
gen type = 0 // interaction
append using "$work\any_pre0"
replace ty = 1 if ty == . // first group
append using "$work\any_pre1"
replace ty = 2 if ty == . // second group

replace t = round(t,0.001)
replace stderr = round(stderr,0.001) // round t values and standard errors for plot
gen hej = -0.037 // just a line where to write estimates

* Scatters, lines for estimates, min95, max95 for each group and line with estimates from interacted model
twoway (scatter estimate m if type == 1, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick)) /// 
(scatter estimate m if type == 2, msize(small) msymbol(Oh) mcolor(blue) alwidth(none) lcolor(black) lwidth(thick)) /// 
(scatter hej m if type == 0, msize(small) msymbol(Oh) mcolor(white) alwidth(none)  mlabel(estimate) mlabp(12) mlabs(vsmall) mlabg(10pt) mlabc(black)) /// 
(scatter hej m if type == 0, msize(small) msymbol(Oh) mcolor(white) alwidth(none) mlabel(t) mlabp(12) mlabs(vsmall) mlabg(0pt) mlabc(black)) ///
(line estimate m if type == 2, fcolor(black%10) alwidth(none) lcolor(blue) lwidth(thick)) ///
(line max95 m if type == 2, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(shortdash)) ///
(line min95 m if type == 2, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(shortdash)) ///
(line estimate m if type == 1, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m if type == 1, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(dash)) ///
(line min95 m if type == 1, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(dash)), $gsw2 legend(order(5 "Mental health care experience" 8 "No mental health care experience") pos(6))  xtitle("Age in months") ytitle("Difference") ylab(-0.04(0.04)0.1, tlcolor(gs10) labgap(6-pt) format(%4.2f)) yline(0, lcolor(gs10) lwidth(medthick)) text(-0.030 0.05 "Coef:", size(vsmall)) text(-0.035 0.05 "t-value:", size(vsmall))
graph export "$out\hetero_anypre.png", replace


// Young mother
use "$work\any1_young",clear
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_young"
replace m = `v' if m == .
}
keep if parm == "interaction"
save "$work\any_young_int", replace

use "$work\any1_young0",clear
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_young0"
replace m = `v' if m == .
}
keep if parm == "peer_treat"
save "$work\any_young0", replace

use "$work\any1_young1",clear
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_young1"
replace m = `v' if m == .
}
keep if parm == "peer_treat"
save "$work\any_young1", replace

use "$work\any_young_int",clear
gen type = 0
append using "$work\any_young0"
replace ty = 1 if ty == .
append using "$work\any_young1"
replace ty = 2 if ty == .

replace t = round(t,0.001)
replace stderr = round(stderr,0.001)
gen hej = -0.05

twoway (scatter estimate m if type == 1, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick)) /// 
(scatter estimate m if type == 2, msize(small) msymbol(Oh) mcolor(blue) alwidth(none) lcolor(black) lwidth(thick)) /// 
(scatter hej m if type == 0, msize(small) msymbol(Oh) mcolor(white) alwidth(none)  mlabel(estimate) mlabp(12) mlabs(vsmall) mlabg(15pt) mlabc(black)) /// 
(scatter hej m if type == 0, msize(small) msymbol(Oh) mcolor(white) alwidth(none) mlabel(t) mlabp(12) mlabs(vsmall) mlabg(6pt) mlabc(black)) ///
(line estimate m if type == 2, fcolor(black%10) alwidth(none) lcolor(blue) lwidth(thick)) ///
(line max95 m if type == 2, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(shortdash)) ///
(line min95 m if type == 2, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(shortdash)) ///
(line estimate m if type == 1, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m if type == 1, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(dash)) ///
(line min95 m if type == 1, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(dash)), $gsw2 legend(order(8 "Mother above 25 years at birth" 5 "Mother below 25 years at birth") pos(6))  xtitle("Age in months") ytitle("Difference") ylab(-0.06(0.02)0.04, tlcolor(gs10) labgap(6-pt) format(%4.2f)) yline(0, lcolor(gs10) lwidth(medthick)) text(-0.0435 0.05 "Coef:", size(vsmall)) text(-0.0465 0.05 "t-value:", size(vsmall))
graph export "$out\hetero_young.png", replace

// First time mothers
use "$work\any1_mfr_first",clear
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_mfr_first"
replace m = `v' if m == .
}
keep if parm == "interaction"
save "$work\any_mfr_first_int", replace

use "$work\any1_mfr_first0",clear
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_mfr_first0"
replace m = `v' if m == .
}
keep if parm == "peer_treat"
save "$work\any_mfr_first0", replace

use "$work\any1_mfr_first1",clear
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_mfr_first1"
replace m = `v' if m == .
}
keep if parm == "peer_treat"
save "$work\any_mfr_first1", replace

use "$work\any_mfr_first_int",clear
gen type = 0
append using "$work\any_mfr_first0"
replace ty = 1 if ty == .
append using "$work\any_mfr_first1"
replace ty = 2 if ty == .

replace t = round(t,0.001)
replace stderr = round(stderr,0.001)
gen hej = -0.03

twoway (scatter estimate m if type == 1, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick)) /// 
(scatter estimate m if type == 2, msize(small) msymbol(Oh) mcolor(blue) alwidth(none) lcolor(black) lwidth(thick)) /// 
(scatter hej m if type == 0, msize(small) msymbol(Oh) mcolor(white) alwidth(none)  mlabel(estimate) mlabp(12) mlabs(vsmall) mlabg(15pt) mlabc(black)) /// 
(scatter hej m if type == 0, msize(small) msymbol(Oh) mcolor(white) alwidth(none) mlabel(t) mlabp(12) mlabs(vsmall) mlabg(6pt) mlabc(black)) ///
(line estimate m if type == 2, fcolor(black%10) alwidth(none) lcolor(blue) lwidth(thick)) ///
(line max95 m if type == 2, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(shortdash)) ///
(line min95 m if type == 2, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(shortdash)) ///
(line estimate m if type == 1, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m if type == 1, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(dash)) ///
(line min95 m if type == 1, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(dash)), $gsw2 legend(order(5 "First-time mothers" 8 "Non-Firsttime mother") pos(6))  xtitle("Age in months") ytitle("Difference") ylab(-0.04(0.02)0.04, tlcolor(gs10) labgap(6-pt) format(%4.2f)) yline(0, lcolor(gs10) lwidth(medthick)) text(-0.0245 0.05 "Coef:", size(vsmall)) text(-0.0267 0.05 "t-value:", size(vsmall))
graph export "$out\hetero_mfr_first.png", replace


// Mental health of exposed mothers
use "$work\any1_epds",clear
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_epds"
replace m = `v' if m == .
}
keep if parm == "interaction"
save "$work\any_epds_int", replace

use "$work\any1_epds0",clear
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_epds0"
replace m = `v' if m == .
}
keep if parm == "peer_treat"
save "$work\any_epds0", replace

use "$work\any1_epds1",clear
gen m = 1
forvalue v = 2/24 {
append using "$work\any`v'_epds1"
replace m = `v' if m == .
}
keep if parm == "peer_treat"
save "$work\any_epds1", replace

use "$work\any_epds_int",clear
gen type = 0
append using "$work\any_epds0"
replace ty = 1 if ty == .
append using "$work\any_epds1"
replace ty = 2 if ty == .

replace t = round(t,0.001)
replace stderr = round(stderr,0.001)
gen hej = -0.03

twoway (scatter estimate m if type == 1, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick)) /// 
(scatter estimate m if type == 2, msize(small) msymbol(Oh) mcolor(blue) alwidth(none) lcolor(black) lwidth(thick)) ///
(scatter hej m if type == 0, msize(small) msymbol(Oh) mcolor(white) alwidth(none)  mlabel(estimate) mlabp(12) mlabs(vsmall) mlabg(15pt) mlabc(black)) /// 
(scatter hej m if type == 0, msize(small) msymbol(Oh) mcolor(white) alwidth(none) mlabel(t) mlabp(12) mlabs(vsmall) mlabg(6pt) mlabc(black)) ///
(line estimate m if type == 2, fcolor(black%10) alwidth(none) lcolor(blue) lwidth(thick)) ///
(line max95 m if type == 2, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(shortdash)) ///
(line min95 m if type == 2, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(shortdash)) ///
(line estimate m if type == 1, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m if type == 1, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(dash)) ///
(line min95 m if type == 1, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(vthin) lpattern(dash)), $gsw2 legend(order(5 "Screening score > 4 and < 11" 8 "Screening score < 5") pos(6))  xtitle("Age in months") ytitle("Difference") ylab(-0.04(0.02)0.06, tlcolor(gs10) labgap(6-pt) format(%4.2f)) yline(0, lcolor(gs10) lwidth(medthick)) text(-0.0235 0.05 "Coef:", size(vsmall)) text(-0.0265 0.05 "t-value:", size(vsmall))
graph export "$out\hetero_epds.png", replace
